﻿<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <title>修改密码</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css" media="all" />
</head>

<body>
    <div style="width:450px;margin:0px auto;padding-top:100px;">
        <form class="layui-form changePwd">
            <div class="layui-form-item">
                <label class="layui-form-label">用户账号:</label>
                <div class="layui-input-block">
                    <input id="userAcc" class="layui-input layui-btn-disabled" name="acc" disabled="disabled"
                        readonly="readonly">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">旧密码</label>
                <div class="layui-input-block">
                    <input type="password" value="" lay-verType="tips" placeholder="请输入旧密码" name="used"
                        lay-verify="sonPwd" class="layui-input pwd">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-block">
                    <input type="password" value="" lay-verType="tips" placeholder="请输入新密码" lay-verify="newPwd"
                        id="oldPwd" class="layui-input pwd">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-block">
                    <input type="password" value="" lay-verType="tips" name="pass" placeholder="请确认密码"
                        lay-verify="newPwd2" class="layui-input pwd">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" type="button" lay-submit=""
                        lay-filter="changePwd">立即修改</button>
                    <button onclick="resetAs()" type="button" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>

    <script src="../../plugins/jquery.min.js"></script>
    <script src="../../js/javascript.util.js"></script>
    <script src="../../js/jquery.util.js"></script>
    <script src="../../plugins/layui/layui.js"></script>
    <script>
        function resetAs() {
            layui.layer.closeAll('tips');
            $(':input:not("#userAcc")').val('');
        }

        $(function () {
            $(':input').keydown(function (event) {
                if (event.keyCode === 13) {
                    $('[lay-filter="changePwd"]').click();
                }
            });

            layui.use(['form', 'layer'], function () {

                var form = layui.form;


                var obj = {
                    type: 'get'
                    , url: "/services/data/getUserInfo.json"
                    , data: '{}'
                    , dataType: 'json'
                    , success: function (result) {
                        if (result.code == 200) {
                            result.data = result.data[0];
                            $('#userAcc').val(result.data.user_acc);
                        } else {
                            layer.msg('获取用户信息失败。');
                            console.warn(result.msg);
                        }
                    }
                    , error: function (ex) {
                        console.warn(ex.responseText);
                    }
                };

                //jquery ajax请求
                jqueryAjax(obj);


                form.verify({
                    sonPwd: function (value) {
                        if (value.length < 1) {
                            return '请填写旧密码';
                        }
                    }
                    , newPwd: function (value) {
                        if (value.length < 1) {
                            return '请填写新密码';
                        } else if (value.length < 5 || value.length > 20) {
                            return '密码应在5位与20位之间';
                        } else if (value === $('[name="used"]').val()) {
                            return "新密码不能和旧密码相同";
                        }
                    }
                    , newPwd2: function (value) {
                        if (value.length < 1) {
                            return '请填写确认密码';
                        } else if (value !== $('#oldPwd').val()) {
                            return '确认密码与新密码不一致';
                        }
                    }
                });


                //监听提交
                form.on('submit(changePwd)', function (data) {

                    $("[lay-filter='changePwd']").addClass("layui-btn-disabled").prop("disabled", true).text("修改中…");
                    layui.layer.closeAll('tips');

                    setTimeout(function () {
                        var suijishu = randomNum(1, 10);
                        //随机成功或者失败
                        if (suijishu > 5) {
                            layer.msg("修改密码成功.(演示而已,别当真.)", { anim: 10, icon: 1 });
                            resetAs();
                        } else {
                            layer.msg('修改密码失败.(演示而已,别当真.)', { anim: 10, icon: 0 });
                        }
                        $("[lay-filter='changePwd']").removeClass("layui-btn-disabled").prop("disabled", false).text("立即修改");
                    }, 1000);

                    // data = data.field;
                    // var obj = {
                    //     user: {
                    //         user_acc: (data.acc == undefined ? '' : data.acc),
                    //         passwd: (data.pass == undefined) ? '' : data.pass
                    //     },
                    //     usedPwd: data.used == undefined ? '' : data.used
                    // }
                    // jqueryAjax({
                    //     type: 'post'
                    //     , url: "/api/home/ChangePwd/"
                    //     , contentType: 'application/json'
                    //     , data: JSON.stringify(obj)
                    //     , headers: {
                    //         'Access-Token': getCookie('loginUser')
                    //     }
                    //     , dataType: 'json'
                    //     , success: function (result) {
                    //         if (result.code == 200) {
                    //             layer.msg("修改密码成功.", { anim: 10, icon: 1 });
                    //             resetAs();
                    //         } else {
                    //             console.warn(result.msg);
                    //             layer.msg(result.msg, { anim: 10, icon: 0 });
                    //         }
                    //     }
                    //     , error: function (ex) {
                    //         console.warn(ex.responseText);
                    //     }
                    //     , complete: function (XMLHttpRequest, textStatus) {
                    // $("[lay-filter='changePwd']").removeClass("layui-btn-disabled").prop("disabled", false).text("立即修改");
                    //     }
                    // });
                    return false;
                });

            });//layui.use结束括号
        });//jquery结束括号
    </script>
</body>

</html>